#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <unistd.h>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
    int fd = 0;
    std::string filename{"/dev/zxchardev"};
    fd = open(filename.c_str(), O_RDWR);
    if(fd < 0){
        printf("open %s failed\n", filename.c_str());
        return 0;
    }
    int count = 10;
    char buf[1024] = {"123"};
//    printf("test: %ld\n", sizeof (buf));
//    return 0;
    while(count-- > 0){
        int num = read(fd, buf, 50);
        printf("read bufsize: %d context: %s\n", num, buf);
        std::string msg{buf};
        int ss = std::to_string(count).size();
        msg.replace(msg.cend() - ss, msg.cend(), std::to_string(count));
//        printf("msg.size= %ld\n", msg.size());
        memcpy(buf, msg.data(), msg.size());
        write(fd, buf, msg.size());
//        sleep(1);
    }
    close(fd);
    return 0;
}
